package com.whq.beans;

import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

public class PasswordUtil {

    public static String encrypt(String password){
        String salt = UUID.randomUUID().toString().replace("-","");
        String finalPassword = DigestUtils.md5DigestAsHex((salt+password).getBytes());
        return salt+finalPassword;
    }

    public static boolean decrypt(String password,String finalPassword){
        if(StringUtils.hasLength(password) || !StringUtils.hasLength(finalPassword)){
            return false;
        }
        if(finalPassword.length() != 64){
            return false;
        }
        String salt = finalPassword.substring(0,32);
        String confirmingPassword = DigestUtils.md5DigestAsHex((salt+password).getBytes());

        return confirmingPassword.equals(finalPassword);
    }


}
